package util;

import java.io.BufferedReader;
import java.io.Reader;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Clob;

public class ClobUtil {

	private Connection conn = null;

	private Statement ps = null;

	private ResultSet rs = null;

	private oracleConnector db = new oracleConnector();

	public String readClob(String id, String table, String keyField,
			String clobField) throws SQLException {

		try {
			conn = db.getConnection();
			ps = conn.createStatement();
			ps.setFetchSize(100);
			String sql = "select " + clobField + " from " + table + " where "
					+ keyField + "=" + id;
			rs = ps.executeQuery(sql);
			// logger.debug(sql);
			rs.next();
			Clob clob = (Clob) rs.getClob(clobField);
			if (clob != null) {
				Reader reader = ((java.sql.Clob) clob).getCharacterStream();
				BufferedReader br = new BufferedReader(reader);
				String value = "";
				String line = "";
				while ((line = br.readLine()) != null) {
					value += line + "\n";
				}
				return value.toString();

			} else {
				return "";

			}
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			if (rs != null) {
				rs.close();
			}
			if (ps != null) {
				ps.close();
			}
			if (conn != null) {
				conn.close();
			}
		}
		return null;
	}

}
